package org.goyo.main.services.base.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.apache.commons.lang.StringUtils;
import org.goyo.common.baseBean.Constants;
import org.goyo.common.exception.DBException;
import org.goyo.common.exception.ServiceException;
import org.goyo.common.utils.DateUtils;
import org.goyo.common.utils.Pager;
import org.goyo.main.domain.dao.base.ICategoriesDao;
import org.goyo.main.domain.entity.BaseObject;
import org.goyo.main.domain.entity.base.Categories;
import org.goyo.main.domain.entity.system.SysDepGroup;
import org.goyo.main.domain.entity.system.SysUser;
import org.goyo.main.services.BaseService;
import org.goyo.main.services.base.ICategoriesService;
import org.goyo.main.web.controller.system.SessionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
@Transactional
public class CategoriesServiceImpl extends BaseService implements ICategoriesService{
  protected Logger logger = LoggerFactory.getLogger(this.getClass());
	@Resource
	private ICategoriesDao categoriesDao;

  @Override
  public String saveOrUpdateInfo(Categories info) throws DBException, ServiceException {/*
    String uuid = null;
    String date = DateUtils.parseDate("yyyy-MM-dd", new Date());
    SysUser user = (SysUser)SessionUtil.getAttribute("user");
    //保存用户数据
    if(info.getUuid() == null || "".equals(info.getUuid())){
        info.setUuid(null);
        info.setStatus(Constants.SAVESTATUS);
        info.setCreateDate(date);
        info.setCreateUser(user);
        info.setAlterDate(date);
        info.setAlterUser(user);
		alterBillNumber(Categories.BIZTYPE, info.getNumber());
        uuid = categoriesDao.save(info);
    }else{
        info.setAlterDate(date);
        info.setAlterUser(user);
        categoriesDao.update(info);
        uuid = info.getUuid();
    }
    
    BaseObject obj = new  BaseObject();
    obj.setCreateDate(date);
    obj.setCreateUser(user);
    obj.setAlterDate(date);
    obj.setAlterUser(user);
    obj.setUuid(uuid);
    obj.setStatus(Constants.SAVESTATUS);
    return obj;
*/

    String pid = info.getParentId();
    if(!StringUtils.isEmpty(pid)){
      categoriesDao.updateByHql("update Categories set leaf=? where id=? ", new Object[]{false,pid});
    }
      String uuid = null;
      if(info.getUuid() == null || "".equals(info.getUuid())){
          info.setUuid(null);
          uuid = categoriesDao.save(info);
      }else{
        uuid = info.getUuid();
        if(categoriesDao.countByHql("select count(*) from Categories where parentId ='"+uuid+"'")>0){
          info.setLeaf(false);
        }else{
          info.setLeaf(true);
        }
        categoriesDao.update(info);
      }
      return uuid;
  }

  @Override
  public void delByUuids(Object[] uuids) throws DBException, ServiceException {
    for(Object uuid : uuids){
      Categories categories  = categoriesDao.get(Categories.class, uuid.toString());
      categoriesDao.delete(categories);
  }
}

  @Override
  public Categories findByUuid(String uuid) throws DBException, ServiceException {
    return categoriesDao.get(Categories.class, uuid);
    }

  @Override
  public Pager<Categories> findByCondition(String where, Pager pager) throws DBException,
      ServiceException {
    return  categoriesDao.findByCond(where, pager);
  }

  @Override
  public List<Categories> getAllCategories() throws DBException, ServiceException {
    List<Categories> list = categoriesDao.getAllCategories();
    return list;
  }

  @Override
  public String getCategoriesSerialNumber() throws DBException, ServiceException {
    int i =0;
    try {
      List list =  categoriesDao.findObjects("select max(CAST(number AS  int)) from Categories", null, null);
      if(null!=list){
       i =(Integer)list.get(0)+1;
      }
    } catch (Exception e) {
      i=0;
      logger.error(e.getMessage(),e);
    }
    String pattern="0000";
    java.text.DecimalFormat df = new java.text.DecimalFormat(pattern);
    return df.format(i)+"";
  }
}
